﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Net;
using BioModel;
using BioDAL;


namespace BioWCF
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
    public class RemoteBookingSystem : IRemoteBookingSystem
    {
        private const string ConnString = @"Data Source=balder.ucn.dk;Initial Catalog=dm76_6;Persist Security Info=True;User ID=dm76_6;Password=MaaGodt";
        //private const string ConnString = @"Data Source=MBP\SQLEXPRESS;Initial Catalog=Biograf;Persist Security Info=True;User ID=sa;Password=MaaGodt";
        public RemoteBookingSystem()
        {
            Console.WriteLine("Welcome");
        }

        #region Connection
        private static IFilmDAL FilmConnection()
        {
            IFilmDAL dal = new FilmDAL();
            dal.OpenConnection(ConnString);
            return dal;
        }

        private static IKundeDAL KundeConnection()
        {
            IKundeDAL dal = new KundeDAL();
            dal.OpenConnection(ConnString);
            return dal;
        }

        private static ISaedeDAL SaedeConnection()
        {
            ISaedeDAL dal = new SaedeDAL();
            dal.OpenConnection(ConnString);
            return dal;
        }

        private static ISalgDAL SalgConnection()
        {
            ISalgDAL dal = new SalgDAL();
            dal.OpenConnection(ConnString);
            return dal;
        }

        private static ISalDAL SalConnection()
        {
            ISalDAL dal = new SalDAL();
            dal.OpenConnection(ConnString);
            return dal;
        }

        private static IForestillingDAL ForestillingConnection()
        {
            IForestillingDAL dal = new ForestillingDAL();
            dal.OpenConnection(ConnString);
            return dal;
        }

        private static IBetalingDAL BetalingConnection()
        {
            IBetalingDAL dal = new BetalingDAL();
            dal.OpenConnection(ConnString);
            return dal;
        }

        #endregion

        #region Film

        public string GetFilm(string title, int year)
        {
            string url;
            if (year == 0)
                url = "http://www.imdbapi.com/?t=" + title;
            else
                url = "http://www.imdbapi.com/?t=" + title + "&y=" + year;

            string result;

            WebClient client = new WebClient();
            result = client.DownloadString(url);

            return result;
        }

        public void NyFilm(string titel, string instruktoer, string beskrivelse, string genre, string laengde, bool aktiv, string billede, bool er3D, double billetPris, List<string> kunstner)
        {
            try
            {
                IFilmDAL iDal = FilmConnection();
                int filmID = iDal.InsertFilm(titel, instruktoer, beskrivelse, genre, laengde, aktiv, billede, er3D, billetPris);
                if (kunstner.Count > 0)
                {
                    iDal.addKunstnerTilFilm(filmID, kunstner);
                }
                iDal.CloseConnection();
            }
            catch (Exception e)
            {
                Console.WriteLine("Fejl in NyFilm: " + e);
            }
        }

        public List<Film> ListFilmByStatus(bool state)
        {
            List<Film> _film = new List<Film>();
            IFilmDAL iDal = FilmConnection();
            _film = iDal.AlleStatusFilm(state);
            return _film;
        }

        public List<Film> AlleFilm()
        {
            List<Film> _film = new List<Film>();
            IFilmDAL iDal = FilmConnection();
            _film = iDal.AlleFilm();
            return _film;
        }

        public List<Film> AlleAktiveFilm(bool status)
        {
            List<Film> alleAktiveFilm = new List<Film>();
            IFilmDAL iDal = FilmConnection();
            alleAktiveFilm = iDal.AlleStatusFilm(status);
            return alleAktiveFilm;
        }

        public Film GetEnFilm(int movID)
        {
            Film _film = new Film();
            IFilmDAL iDal = FilmConnection();
            _film = iDal.GetEnFilm(movID);
            return _film;
        }

        public Film GetEnFilmFraTitel(string titel)
        {
            Film _film = new Film();
            IFilmDAL iDal = FilmConnection();
            _film = iDal.GetEnFilmFraTitel(titel);
            return _film;
        }
        #endregion

        #region Kunde
        public void InsertKunde(int telefonNr, string navn, string email, string password)
        {
            try
            {
                IKundeDAL iDal = KundeConnection();
                iDal.InsertKunde(telefonNr, navn, email, password);
                iDal.CloseConnection();
            }
            catch (Exception e)
            {
                Console.WriteLine("Fejl i oprettelse af kunde: " + e);
            }
        }

        public void EditKunde(int telefonNr, string navn, string email)
        {
            IKundeDAL iDal = KundeConnection();
            iDal.EditKunde(telefonNr, navn, email);
            iDal.CloseConnection();
        }

        public List<Kunde> AlleKunder()
        {
            IKundeDAL iDal = KundeConnection();
            return iDal.AlleKunder();
        }

        public Kunde GetEnKunde(int tlf)
        {
            IKundeDAL iDal = KundeConnection();
            return iDal.GetEnKunde(tlf);
        }
        #endregion

        #region Salg
        public void NySalg(DateTime datoOprettet, int antalBilletter, string salgsType, double totalPris, int forestilling, int kundeTlf, List<Saede> s)
        {
            ISalgDAL iDal = SalgConnection();
            iDal.InsertSalg(datoOprettet, antalBilletter, salgsType, totalPris, forestilling, kundeTlf, s);
            iDal.CloseConnection();
        }

        public void EditSalg(int telefonNr, string salgsType)
        {
            ISalgDAL iDal = SalgConnection();
            iDal.EditSalg(telefonNr, salgsType);
            iDal.CloseConnection();
        }

        public List<Salg> GetAlleSalg(int telefonNr)
        {
            List<Salg> alleSalg = new List<Salg>();
            ISalgDAL iDal = SalgConnection();
            alleSalg = iDal.GetAlleSalg(telefonNr);
            return alleSalg;
        }

        public Salg GetEnSalg(int salgId)
        {
            ISalgDAL iDal = SalgConnection();
            return iDal.GetEnSalg(salgId);
        }

        #endregion

        #region Sal
        public int InsertSal(int antalPladser, bool er3D)
        {
            try
            {
                ISalDAL iDal = SalConnection();
                return iDal.InsertSal(antalPladser, er3D);
            }
            catch (Exception)
            {
                Console.WriteLine("Fejl i oprettelse af Sal");
                return -1;
            }
        }

        public void EditSal(int salNr, bool er3D)
        {
            ISalDAL iDal = SalConnection();
            iDal.EditSal(salNr, er3D);
            iDal.CloseConnection();
        }

        public List<Sal> AlleSal()
        {
            List<Sal> salListe = new List<Sal>();
            ISalDAL iDal = SalConnection();
            salListe = iDal.AlleSal();
            return salListe;
        }

        public List<Sal> Alle3DSal(bool status)
        {
            List<Sal> alle3DSal = new List<Sal>();
            ISalDAL iDal = SalConnection();
            alle3DSal = iDal.Alle3DSal(status);
            return alle3DSal;
        }

        public Sal GetEnSal(int salNr)
        {
            ISalDAL iDal = SalConnection();
            return iDal.GetEnSal(salNr);
        }

        public void SletEnSal(int salId)
        {
            ISalDAL iDal = SalConnection();
            iDal.SletEnSal(salId);
            iDal.CloseConnection();
        }

        
        #endregion

        #region Forestilling
        public int InsertForestilling(DateTime fremvisningsDato, string start, string slut, int film, int sal)
        {
            try
            {
                IForestillingDAL iDal = ForestillingConnection();
                return iDal.InsertForestilling(fremvisningsDato, start, slut, film, sal);
            }
            catch (Exception)
            {
                Console.WriteLine("Fejl i oprettelse af forestilling");
                return -1;
            }
        }

        public void EditForestilling(int forestillingId, DateTime fremvisningsDato, string start, string slut, int film, int sal)
        {
            IForestillingDAL iDal = ForestillingConnection();
            iDal.EditForestilling(forestillingId, fremvisningsDato, start, slut, film, sal);
            iDal.CloseConnection();
        }

        public List<Forestilling> AlleForestillinger()
        {
            List<Forestilling> forestillingListe = new List<Forestilling>();
            IForestillingDAL iDal = ForestillingConnection();
            forestillingListe = iDal.AlleForestillinger();
            return forestillingListe;
        }

        public List<Forestilling> GetForestillingerFraFilmId(int filmId)
        {
            List<Forestilling> forestillingListe = new List<Forestilling>();
            IForestillingDAL iDal = ForestillingConnection();
            forestillingListe = iDal.GetForestillingerFraFilmId(filmId);
            return forestillingListe;
        }

        public List<Forestilling> GetForestillingerFraDato(DateTime dato)
        {
            List<Forestilling> forestillingListe = new List<Forestilling>();
            IForestillingDAL iDal = ForestillingConnection();
            forestillingListe = iDal.GetForestillingerFraDato(dato);
            return forestillingListe;
        }

        public List<Forestilling> GetForestillingerFraDatoOgFilmId(DateTime dato, int filmId)
        {
            List<Forestilling> forestillingListe = new List<Forestilling>();
            IForestillingDAL iDal = ForestillingConnection();
            forestillingListe = iDal.GetForestillingerFraDatoOgFilmId(dato, filmId);
            return forestillingListe;
        }

        public List<Forestilling> GetForestillingerFraDatoFilmIdOgTid(DateTime dato, int filmId, string tid)
        {
            List<Forestilling> forestillingListe = new List<Forestilling>();
            IForestillingDAL iDal = ForestillingConnection();
            forestillingListe = iDal.GetForestillingerFraDatoFilmIdOgTid(dato, filmId, tid);
            return forestillingListe;
        }

        public Forestilling GetEnForestilling(int forestillingId)
        {
            IForestillingDAL iDal = ForestillingConnection();
            return iDal.GetEnForestilling(forestillingId);
        }

        public Forestilling GetForestillingsIdFraFrilmIdDatoOgTid(int filmId, DateTime dato, string tid)
        {
            IForestillingDAL iDal = ForestillingConnection();
            return iDal.GetForestillingsIdFraFrilmIdDatoOgTid(filmId, dato, tid);
        }

        public Forestilling GetEnForestillingAfDatoOgTid(DateTime dato, string tid)
        {
            IForestillingDAL iDal = ForestillingConnection();
            return iDal.GetEnForestillingAfDatoOgTid(dato, tid);
        }

        public Forestilling GetForestillingId(int filmId, DateTime forestillingsDato, string startTid, int salId)
        {
            IForestillingDAL iDal = ForestillingConnection();
            return iDal.GetForestillingId(filmId, forestillingsDato, startTid, salId);
        }
        #endregion

        #region Betaling
        public int InsertBetaling(DateTime betalingsDato, bool betalingsStatus)
        {
            try
            {
                IBetalingDAL iDal = BetalingConnection();
                return iDal.InsertBetaling(betalingsDato, betalingsStatus);
            }
            catch (Exception)
            {
                Console.WriteLine("Fejl i oprettelse af betaling");
                return -1;
            }
        }
        public void EditBetaling(int betalingsId, DateTime betalingsDato, bool betalingsStatus)
        {
            IBetalingDAL iDal = BetalingConnection();
            iDal.EditBetaling(betalingsId, betalingsDato, betalingsStatus);
            iDal.CloseConnection();
        }
        public List<Betaling> AlleBetalinger()
        {
            List<Betaling> betalingsListe = new List<Betaling>();
            IBetalingDAL iDal = BetalingConnection();
            betalingsListe = iDal.AlleBetalinger();
            return betalingsListe;
        }
        public Betaling GetEnBetaling(int betalingsId)
        {
            IBetalingDAL iDal = BetalingConnection();
            return iDal.GetEnBetaling(betalingsId);
        }
        #endregion

        #region Saede
        public void InsertSaede(int saedeNr, int raekkeNr, int forestillingsId)
        {
            try
            {
                ISaedeDAL iDal = SaedeConnection();
                iDal.InsertSaede(saedeNr, raekkeNr, forestillingsId);
                iDal.CloseConnection();
            }
            catch (Exception)
            {
                Console.WriteLine("Fejl i oprettelse af sæde");
            }
        }

        public void EditSaede(int saedeId, string saedeStatus)
        {
            ISaedeDAL iDal = SaedeConnection();
            iDal.EditSaede(saedeId, saedeStatus);
            iDal.CloseConnection();
        }
        public List<Saede> AlleSaeder()
        {
            List<Saede> saedeListe = new List<Saede>();
            ISaedeDAL iDal = SaedeConnection();
            saedeListe = iDal.AlleSaeder();
            return saedeListe;
        }
        public List<Saede> AlleSaederFraForestilling(int forestillingsId)
        {
            List<Saede> saedeListe = new List<Saede>();
            ISaedeDAL iDal = SaedeConnection();
            saedeListe = iDal.AlleSaederFraForestilling(forestillingsId);
            return saedeListe;
        }
        public Saede GetEnSaede(int saedeId)
        {
            ISaedeDAL iDal = SaedeConnection();
            return iDal.GetEnSaede(saedeId);
        }
        public void SletEnSaede(int sal)
        {
            ISaedeDAL iDal = SaedeConnection();
            iDal.SletEnSaede(sal);
            iDal.CloseConnection();
        }

        public int GetMaxSal(string kolonneNavn, int sal)
        {
            ISaedeDAL iDal = SaedeConnection();
            return iDal.GetMaxSal(kolonneNavn, sal);
        }
        #endregion
    }
}
